package com.changpay.zywy.ui.activity.mine

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.app.App
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.utils.ImageLoader
import com.changpay.zywy.utils.Preference
import com.changpay.zywy.utils.StringUtil
import com.ruffian.library.widget.RCheckBox
import kotlinx.android.synthetic.main.activity_pay_password.*
import kotlinx.android.synthetic.main.title_layout.*
import java.lang.StringBuilder

class PayPasswordActivity : BaseActivity(), IView {


    var stringBuilder: StringBuilder = StringBuilder()
    val checkBoxList = arrayListOf<RCheckBox>()
    /**
     * local username
     */
    var member_mobile: String by Preference(Constant.MEMBER_MOBILE, "")
    var head_image: String by Preference(Constant.HEAD_IMAGE, "")
    var true_name: String by Preference(Constant.TRUE_NAME, "")
    var card_id: String? = null


    var imp_type: Int = 0

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
        while (!stringBuilder.isEmpty()) {
            stringBuilder.delete(stringBuilder.length - 1, stringBuilder.length)
            checkBoxList[stringBuilder.length].isChecked = false
        }
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.number_one -> takePlay("1")
            R.id.number_two -> takePlay("2")
            R.id.number_three -> takePlay("3")
            R.id.number_four -> takePlay("4")
            R.id.number_five -> takePlay("5")
            R.id.number_six -> takePlay("6")
            R.id.number_seven -> takePlay("7")
            R.id.number_eight -> takePlay("8")
            R.id.number_nine -> takePlay("9")
            R.id.number_zero -> takePlay("0")
            R.id.number_sure -> takePlay("sure")
            R.id.number_back -> takePlay("back")
        }
    }


    override fun attachLayoutRes(): Int {
        return R.layout.activity_pay_password
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = intent.getStringExtra("activity_title")
        check_title.text = intent.getStringExtra("title")
        check_message.text = intent.getStringExtra("message")
        imp_type = intent.getIntExtra("imp_type", 0)
        ImageLoader.loadAvatar(head_image, check_head)
        check_name.text = true_name
        check_tv_phone.text = StringUtil.PhoneToPass(member_mobile)
        iv_back.setOnClickListener(onClickListener)
        number_one.setOnClickListener(onClickListener)
        number_two.setOnClickListener(onClickListener)
        number_three.setOnClickListener(onClickListener)
        number_four.setOnClickListener(onClickListener)
        number_five.setOnClickListener(onClickListener)
        number_six.setOnClickListener(onClickListener)
        number_seven.setOnClickListener(onClickListener)
        number_eight.setOnClickListener(onClickListener)
        number_nine.setOnClickListener(onClickListener)
        number_zero.setOnClickListener(onClickListener)
        number_sure.setOnClickListener(onClickListener)
        number_back.setOnClickListener(onClickListener)
        checkBoxList.add(cb_one)
        checkBoxList.add(cb_two)
        checkBoxList.add(cb_three)
        checkBoxList.add(cb_four)
        checkBoxList.add(cb_five)
        checkBoxList.add(cb_six)
    }

    override fun start() {


    }


    private fun takePlay(type: String) {
        when (type) {
            "back" -> {
                if (stringBuilder.isEmpty()) return
                stringBuilder.delete(stringBuilder.length - 1, stringBuilder.length)
                checkBoxList[stringBuilder.length].isChecked = false
            }
            "sure" -> {
                if (stringBuilder.length < 6) {
                    showToast("长度不足6位")
                    return
                }
                sure(stringBuilder.toString())
            }
            else -> {
                if (stringBuilder.length >= 6) return
                stringBuilder.append(type)
                checkBoxList[stringBuilder.length - 1].isChecked = true
            }
        }

    }


    private fun sure(password: String) {
        when (imp_type) {
            0 -> {   // 解绑银行卡
                card_id = intent.getStringExtra("card_id")
                val params = HashMap<String, String>()
                params["key"] = token
                params["password"] = password
                RetrofitHelper.service.check_pd_pwd(params).sss(this)
                {
                    showToast(it.result.toString())
                    val param = HashMap<String, String>()
                    param["key"] = token
                    param["card_id"] = card_id.toString()
                    RetrofitHelper.service.del_bank_card(param).sss(this)
                    {
                        showToast(it.result.toString())
                        finish()
                        (App.instance as App).finishTopActivity(2)
                    }
                }
            }

            1 -> {   // 绑定银行卡
                val params = HashMap<String, String>()
                params["key"] = token
                params["password"] = password
                RetrofitHelper.service.check_pd_pwd(params).sss(this)
                {
                    startActivity(BindBankCardActivity::class.java)
                }
            }
            2 -> {   // 验证密码
                val params = HashMap<String, String>()
                params["key"] = token
                params["password"] = password
                RetrofitHelper.service.check_pd_pwd(params).sss(this)
                {
                    Intent().run {
                        putExtra("password", password)
                        setResult(200, this)
                        finish()
                    }

                }
            }

        }
    }

}
